package com.dispatcher.view;

import java.util.Map;

import com.dispatcher.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PublicTopView extends RelativeLayout {
	private TextView public_text_left;
	private ImageButton public_imgbtn_left;
	private ImageButton topleft_one_line_type;
	private ImageButton topleft_two_line_type;
	private ImageButton topright_one_line_type;
	private ImageButton topright_two_line_type;
	private ImageButton public_imgbtn_right;
	private Button public_btn_left;
	private Button public_btn_right;

	private TextView public_title_text;

	private Context context;
	private SharedPreferences spf;
	private Map<String, Object> mapvalue;
	View view;

	public PublicTopView(Context context, AttributeSet attrs, int defStyle) {
		// TODO Auto-generated constructor stub
		super(context, attrs, defStyle);
		this.context = context;
		init();

	}

	public PublicTopView(Context context, AttributeSet attrs) {
		// TODO Auto-generated constructor stub
		super(context, attrs);
		this.context = context;
		init();
	}

	public PublicTopView(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		this.context = context;
		init();
	}

	void init() {

		findView();
		hide();
	}

	void findView() {

		view = View.inflate(context, R.layout.public_head, null);
		public_text_left = (TextView) view.findViewById(R.id.public_text_left);
		public_imgbtn_left = (ImageButton) view
				.findViewById(R.id.public_button_left);
		public_imgbtn_right = (ImageButton) view
				.findViewById(R.id.public_button_right);
		topleft_one_line_type = (ImageButton) view
				.findViewById(R.id.topleft_one_line_type);
		topleft_two_line_type = (ImageButton) view
				.findViewById(R.id.topleft_two_line_type);
		topright_one_line_type = (ImageButton) view
				.findViewById(R.id.topright_one_line_type);
		topright_two_line_type = (ImageButton) view
				.findViewById(R.id.topright_two_line_type);
		public_title_text = (TextView) view.findViewById(R.id.public_text);
		public_btn_left = (Button) view
				.findViewById(R.id.public_button_leftbutton);
		public_btn_right = (Button) view
				.findViewById(R.id.public_button_rightbutton);
		addView(view);

	}

	void hide() {

		public_title_text.setVisibility(View.GONE);
		topleft_one_line_type.setVisibility(View.GONE);
		topleft_two_line_type.setVisibility(View.GONE);
		topright_one_line_type.setVisibility(View.GONE);
		topright_two_line_type.setVisibility(View.GONE);
		public_imgbtn_left.setVisibility(View.GONE);
		public_imgbtn_right.setVisibility(View.GONE);
		public_btn_left.setVisibility(View.GONE);
		public_btn_right.setVisibility(View.GONE);
	}

	/**
	 * setting left Button picture
	 * 
	 * @param id
	 */
	public void setLeftPicture(int id) {
		if (id == 0) {

		} else {
			public_imgbtn_left.setImageResource(id);
			public_imgbtn_left.setVisibility(View.VISIBLE);
			topleft_one_line_type.setVisibility(View.VISIBLE);
		}
	}
	
	public void setLeftButtonText(String text) {
		if (text == null) {

		} else {
			public_btn_left.setText(text);
			topleft_two_line_type.setVisibility(View.VISIBLE);
			public_btn_left.setVisibility(View.VISIBLE);

		}
	}

	public void setLeftButtonClickListener(OnClickListener lis) {

		if (lis != null) {

			public_btn_left.setOnClickListener(lis);
		}
	}

	/**
	 * setting right Button picture
	 * 
	 * @param id
	 */
	public void setRightPicture(int id) {
		if (id == 0) {

		} else {
			public_btn_right.setVisibility(View.GONE);
			public_imgbtn_right.setImageResource(id);
			public_imgbtn_right.setVisibility(View.VISIBLE);
			topright_one_line_type.setVisibility(View.VISIBLE);
		}
	}

	public void setRightButtonText(String text) {
		if (text == null) {

		} else {
			public_btn_right.setText(text);
			topright_two_line_type.setVisibility(View.VISIBLE);
			public_btn_right.setVisibility(View.VISIBLE);

		}
	}

	/**
	 * setting right Button Background
	 * 
	 * @param text
	 */
	public void setTitleText(String text) {
		if (text == null) {

		} else {

			public_title_text.setText(text);
		}
		public_title_text.setVisibility(View.VISIBLE);
	}
	// action bar 没有左侧按钮，则将中间textview移到左侧
	public void setTitleTextToLeft(String text) {
		if(text == null) {
			return;
		} else {
//			((RelativeLayout)public_title_text.getParent()).removeView(public_title_text);
//			removeView(public_title_text);
//			android.widget.LinearLayout.LayoutParams param=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
//			public_title_text.setLayoutParams(param);
//			float den=context.getResources().getDisplayMetrics().density;
//			public_title_text.setPadding((int)(10*den), 0, (int)(10*den), 0);
//			LinearLayout layout=(LinearLayout)findViewById(R.id.topleft_btns);
//			public_title_text.setText(text);
//			public_title_text.setVisibility(View.VISIBLE);
//			layout.addView(public_title_text, 0);
			
			RelativeLayout layout = (RelativeLayout)public_title_text.getParent();
			((RelativeLayout)public_title_text.getParent()).removeView(public_title_text);
			android.widget.RelativeLayout.LayoutParams param=new android.widget.RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
			public_title_text.setLayoutParams(param);
			float den=context.getResources().getDisplayMetrics().density;
			public_title_text.setPadding((int)(10*den), 0, (int)(10*den), 0);
			public_title_text.setText(text);
			public_title_text.setVisibility(View.VISIBLE);
			layout.addView(public_title_text, 0);
		}	
	}

	/**
	 * leftButton listener
	 * 
	 * @param lis
	 */
	public void setLeftClickListener(OnClickListener lis) {
		if (lis == null) {

		} else {
			public_imgbtn_left.setOnClickListener(lis);
		}
	}
	public void addRightButton(String text,OnClickListener lis){
		Button btn=new Button(getContext());
		android.widget.LinearLayout.LayoutParams param=new android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
		btn.setLayoutParams(param);
		float den=context.getResources().getDisplayMetrics().density;
		btn.setPadding((int)(10*den), 0, (int)(10*den), 0);
		btn.setBackgroundResource(R.drawable.public_top_btn_selector);
		LinearLayout layout=(LinearLayout)findViewById(R.id.topright_btns);
		btn.setText(text);
		btn.setTextColor(getResources().getColor(R.color.white));
		btn.setOnClickListener(lis);
		layout.addView(btn, 0);
		
		View line=new View(context);
		param=new android.widget.LinearLayout.LayoutParams((int)(2*den),android.view.ViewGroup.LayoutParams.FILL_PARENT);
		line.setLayoutParams(param);
		line.setBackgroundResource(R.drawable.top_line);
		layout.addView(line, 0);
		
	}
	public void addRightImgButton(int imgsrc,OnClickListener lis){
		ImageButton btn=new ImageButton(getContext());
		android.widget.LinearLayout.LayoutParams param=new android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
		btn.setLayoutParams(param);
		float den=context.getResources().getDisplayMetrics().density;
		btn.setPadding((int)(10*den), 0, (int)(10*den), 0);
		btn.setBackgroundResource(R.drawable.public_top_btn_selector);
		LinearLayout layout=(LinearLayout)findViewById(R.id.topright_btns);
		btn.setImageResource(imgsrc);
		btn.setOnClickListener(lis);
		layout.addView(btn, 0);
		
		View line=new View(context);
		param=new android.widget.LinearLayout.LayoutParams((int)(2*den),android.view.ViewGroup.LayoutParams.FILL_PARENT);
		line.setLayoutParams(param);
		line.setBackgroundResource(R.drawable.top_line);
		layout.addView(line, 0);
		
	}
	/**
	 * Right listener
	 * 
	 * @param lis
	 */
	public void setRightClickListener(OnClickListener lis) {
		if (lis == null) {

		} else {
			public_imgbtn_right.setOnClickListener(lis);
		}
	}

	public void setRightButtonClickListener(OnClickListener lis) {
		if (lis == null) {

		} else {
			public_btn_right.setOnClickListener(lis);
		}
	}

	public TextView getTitleText() {

		return public_title_text;
	}
	
	public TextView getLeftTitleText() {
		return public_text_left;
	}

}
